/*
Package seeders
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/4/25 18:18
@File     : users_seeder.go
@Project  : gohub
@Desc     :
*/
package seeders

import (
	"fmt"
	"gohub/database/factories"
	"gohub/pkg/console"
	"gohub/pkg/logger"
	"gohub/pkg/seed"
	"gorm.io/gorm"
)

func init() {
	//添加Seeder
	seed.Add("SeedUsersTable", func(db *gorm.DB) {
		//创建10个用户
		users := factories.MakeUsers(10)

		//批量创建用户（批量创建不会调用模型钩子）
		result := db.Table("users").Create(&users)

		//记录错误
		if err := result.Error; err != nil {
			logger.LogIf(err)
			return
		}

		//打印运行情况
		console.Success(fmt.Sprintf("Table [%v] %v rows seeded", result.Statement.Table, result.RowsAffected))
	})
}
